package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.dao.GoodInfoMapper;
import com.zhentao.dao.GoodMapper;
import com.zhentao.dao.ShoppingCartMapper;
import com.zhentao.dao.UserAccountMaaper;
import com.zhentao.entity.user.Good;
import com.zhentao.entity.user.GoodsInfo;
import com.zhentao.entity.user.ShoppingCart;
import com.zhentao.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class GoodServiceImpl extends ServiceImpl<GoodMapper, Good>  implements GoodService {
//    @Autowired
//    GoodInfoMapper goodInfoMapper;

//    @Override
//    public List<GoodsInfo> goodSearch(String categoryName) {
//        // 非空校验
//        if (StringUtils.isBlank(categoryName)) {
//            throw new IllegalArgumentException("名称不能为空");
//        }
//
//        // 查询条件
////        LambdaQueryWrapper<Good> queryWrapper = new LambdaQueryWrapper<>();
////        queryWrapper.like(Good::getCategoryName, categoryName);
//        QueryWrapper<GoodsInfo> queryWrapper = new QueryWrapper<>();
//        queryWrapper.like("goods_name", categoryName);
//        return goodInfoMapper.selectList(queryWrapper); // 使用 GoodsInfoMapper
//    }

    @Override
    public List<String> goodClass() {
       ArrayList<String> goodsName = new ArrayList<>();
       QueryWrapper<Good> wrapper=new QueryWrapper<>();
        List<Good> list=this.baseMapper.selectList(wrapper);
        //种类集合获取种类名称给新的string集合
        for (Good good : list) {
            String goodName=good.getCategoryName();
            goodsName.add(goodName);
        }

        return goodsName;
    }

    @Override
    public List<GoodsInfo> classGoods(String categoryName) {
//        通过种类的名称查种类的id
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("category_name",categoryName);
        Good good=this.baseMapper.selectOne(wrapper);
        int id=good.getParentId();

        QueryWrapper wrapperGoods=new QueryWrapper();
        wrapperGoods.eq("parent_id",id);
        List<GoodsInfo> goodList=this.baseMapper.selectList(wrapperGoods);
        return goodList;
    }
    //    @Override
//    public List<Good> classGoods(String categoryName) {
//        //通过种类的名称查种类的id
//        QueryWrapper wrapper=new QueryWrapper();
//        wrapper.eq("category_name",categoryName);
//        Good good=this.baseMapper.selectOne(wrapper);
//        int id=good.getCategoryId();
//        //种类id查找对应种类的商品
//        QueryWrapper wrapperGoods=new QueryWrapper();
//        wrapperGoods.eq("parent_id",id);
//        List<Good> goodList=this.baseMapper.selectList(wrapperGoods);
//
//        return goodList;
//    }
    @Autowired
    private GoodInfoMapper goodInfoMapper;
    @Autowired
    private  UserAccountMaaper userAccountMaaper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public String addToCar(BigInteger userId, BigInteger goodsId, int quantity,int guigeId) {
        //1.判断库存
            //1.2根据goodsId查询商品库存
            GoodsInfo goodsInfo=this.goodInfoMapper.selectById(goodsId);
            int stock=goodsInfo.getStock();
            if(quantity>stock){
                return "库存不足";
            }else {
                //2判断用户的购物车有无该商品
                QueryWrapper<ShoppingCart> wrapper=new QueryWrapper<>();
                wrapper.eq("user_id",userId)
                        .eq("good_id",goodsId)
                        .eq("guige_id",guigeId);
                List<ShoppingCart> list=this.shoppingCartMapper.selectList(wrapper);
                    //2.2有该商品则数量加
                    if(list!=null && list.size()!=0){
                        ShoppingCart shoppingCart=list.get(0);
                        shoppingCart.setQuantity(shoppingCart.getQuantity()+1);
                        this.shoppingCartMapper.updateById(shoppingCart);
                    }
                    //2.3没有该商品新增该订单数据
                    else{
                        GoodsInfo newGood=this.goodInfoMapper.selectById(goodsId);
//                        UserAccount newUser=this.userAccountMaaper.selectById(userId);
                        ShoppingCart shoppingCart=new ShoppingCart();
                        shoppingCart.setName(newGood.getGoodsName());
                        shoppingCart.setUserId(userId);
                        shoppingCart.setGoodId(goodsId);
                        shoppingCart.setGuigeId(guigeId);
                        shoppingCart.setQuantity(quantity);
                        shoppingCart.setAmount(newGood.getCurrentPrice()*quantity);
                        shoppingCart.setCreateTime(new Date());
                        this.shoppingCartMapper.insert(shoppingCart);
                    }
                return "添加成功";
            }
    }

    @Override
    public String cleanCar(BigInteger userId) {
        QueryWrapper<ShoppingCart> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<ShoppingCart> list=this.shoppingCartMapper.selectList(wrapper);
        for (ShoppingCart shoppingCart : list) {
            this.shoppingCartMapper.deleteById(shoppingCart);
        }
        return "已清空";
    }
}
